package com.le.tester.book.concurrentjavafight.l_14;

/**
 * createTime：2024/6/18 15:03
 * description：tryAcquireShared and tryReleaseShared
 */
public class SemaphoreSelf {

    protected int tryAcquireShared(int acquires) {
        while (true) {
            int available = getState();
            int remaining = available - acquires;

            if (remaining < 0 || compareAndSetState(available, remaining)) {
                return remaining;
            }
        }
    }

    protected boolean tryReleaseShared(int releases) {
        while (true) {
            int p = getState();
            if (compareAndSetState(p, p + releases)) {
                return true;
            }
        }
    }

    private boolean compareAndSetState(int available, int remaining) {
        return true;
    }

    public int getState() {
        return 0;
    }
}
